package com.hot12345.common.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 常用操作Map类的工具类
 * @author wuzongbao
 * 2011-7-18
 */
public class MapUtil {
	private final static Log log = LogFactory.getLog(MapUtil.class);

	/**
	 * 获取运行的所有类型基本信息
	 * 
	 * @author wuzongbao
	 * @date 2012-7-16 下午03:48:44
	 */
	public static void getCaller() {  
        StackTraceElement stack[] = (new Throwable()).getStackTrace();  
        for (int i = 0; i < stack.length; i++) {  
        	
            StackTraceElement s = stack[i];  
            System.out.format(" ClassName:%d\t%s\n", i, s.getClassName());  
            System.out.format("MethodName:%d\t%s\n", i, s.getMethodName());  
            System.out.format("  FileName:%d\t%s\n", i, s.getFileName());  
            System.out.format("LineNumber:%d\t%s\n\n", i, s.getLineNumber());  
        }  
    }  
	
	/**
	 * 将Map对象的关键字和值输出，级别为error
	 * @param map
	 */
	public static void printMap(Map map){
		String parentClass = new Throwable().getStackTrace()[1].getClassName(); //获取调用者的类名  
//	    String parentClass_method_name = new Throwable().getStackTrace()[1].getMethodName(); //获取调用者的方法名  
	    int parentClass_LineNumber = new Throwable().getStackTrace()[1].getLineNumber(); //获取调用者的方法名
	    log.error("lineNum:" +parentClass_LineNumber+ " ParentClass:" + parentClass);
		if(map==null){
			log.error("map is null");
			return;
		}
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			log.error("key:"+key + " , value:" + value);
		}
	}
	
	/**
	 * 将Map对象的关键字和值输出，级别为error
	 * @param map
	 * @param logLevel 日志输出级别 error,info,debug,warn
	 */
	public static void printMap(Map map,String logLevel){
		String parentClass = new Throwable().getStackTrace()[1].getClassName(); //获取调用者的类名  
//	    String parentClass_method_name = new Throwable().getStackTrace()[1].getMethodName(); //获取调用者的方法名  
	    int parentClass_LineNumber = new Throwable().getStackTrace()[1].getLineNumber(); //获取调用者的方法名
	    if("error".equals(logLevel)){
	    	log.error("lineNum:" +parentClass_LineNumber+ " ParentClass:" + parentClass);
		} else if("debug".equals(logLevel)){
			log.debug("lineNum:" +parentClass_LineNumber+ " ParentClass:" + parentClass);
		} else if("warn".equals(logLevel)){
			log.warn("lineNum:" +parentClass_LineNumber+ " ParentClass:" + parentClass);
		} else if("info".equals(logLevel)){
			log.info("lineNum:" +parentClass_LineNumber+ " ParentClass:" + parentClass);
		}
	    
		if(map==null){
			return;
		}
		if(logLevel==null){
			logLevel = "error";//默认为error级别
		}
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			if("error".equals(logLevel)){
				log.error("key:"+key + " , value:" + value);
			} else if("debug".equals(logLevel)){
				log.debug("key:"+key + " , value:" + value);
			} else if("warn".equals(logLevel)){
				log.warn("key:"+key + " , value:" + value);
			} else if("info".equals(logLevel)){
				log.info("key:"+key + " , value:" + value);
			}
			
		}
	}
	
	/**
	 * 删除sql查询中的指定参数：pageSize, startNo
	 * 对于统计总数的sql中是不需要这些参数的。
	 * @author wuzongbao
	 * @date 2012-6-22 下午06:38:39
	 */
	public static void removeSqlParamsKey(Map<String,Object> params){
		if(params!=null){
			params.remove("pageSize");
			params.remove("startNo");
		}
	}
}
